<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>我的购物车</title>
  <meta content="width=device-width,initial-scale=1.0,maximum-scale=1.0,user-scalable=0" name="viewport"/>
  <meta content="yes" name="apple-mobile-web-app-capable"/>
  <meta content="black" name="apple-mobile-web-app-status-bar-style"/>
  <meta content="telephone=no" name="format-detection"/>
  <script src="http://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script>
  <script src="__STATIC__/layer/layer.js"></script>
</head>
<body>
<form action="{:url('MyCart/pre_create_order')}" method="post" id="form">
    {if condition="!empty($goods)"}
        {foreach name="goods" item="v"}
                <div id="{$v.spec_id}">
                    <input type="checkbox" name="ids[]" value="{$v.spec_id}">{$v.spec_id}
                    <a href="{:url('GoodsList/goods_detail')}?id={$v.goods_id}"><img src="{$v.logo}" width="100px;" height="100px;"><br/></a>
                    商品名称：<span>{$v.name}</span><br/>
                    属性名称：<span>{$v.title}</span><br/>
                    商品单价：<span id="price_{$v.spec_id}">{if condition="$v.is_member eq 0"}{$v.price}{else /}{$v.member_price}{/if}</span><br/>
                    商品数量：<input type="button" onclick="reduce(this,{$v.spec_id})" value="-">
                        <span id="goods_sum_{$v.spec_id}">{$v.num}</span>
                    <input type="button" onclick="increase(this,{$v.spec_id})" value="+"><br/>
                    商品总价：<span id="goods_price_{$v.spec_id}">{$v.total_price}</span><br/>
                    <input type="button" onclick="del(this,{$v.spec_id})" value="删除">
                </div>

        {/foreach}
    <input type="button" value="清空" onclick="qk_cart();">
    <input type="submit" value="提交" onclick="return checkform();">
    {else/}
        购物车空空如也~~~
    {/if}
  </form>
</body>
</html>
<script>
  /**
   * 减少商品数量
   * @param  id  order_cart的主键id
   */
  function reduce(obj,id){
      $.post("{:url('MyCart/reduce_cart')}",
          {id:id},
          function(data){
              var goods_sum = parseInt($("#goods_sum_"+id).text());
//              console.log(data);
              if(data.data.code == 1){
                  if(goods_sum == 1){                       //删除商品
//                      $('#form').text('购物车空空如也~~~');
                      location.reload();
                  }else{
                      var price = parseFloat($("#price_"+id).text());
                      goods_sum = goods_sum-1;
                      var total_price = parseFloat(goods_sum*price);
                      $("#goods_sum_"+id).text(goods_sum);
                      $("#goods_price_"+id).text(total_price);
                  }
              }else if(data.data.code == 0){        //购物车数量超出商品库存，自动恢复至商品最大库存数量
                  alert(data.msg);
                  var price = parseFloat($("#price_"+id).text());       //商品单价
                  goods_sum = parseInt(data.data.goods-sum);            //购物车商品数量
                  var total_price = parseFloat(goods_sum*price);
                  $("#goods_sum_"+id).text(goods_sum);
                  $("#goods_price_"+id).text(total_price);
              }else if(data.data.code == -1){       //该商品已经下架，将从购物车中移除该商品
                  alert(data.msg);
                  $(obj).parent().remove();         //移除商品信息
              }
          }
      );
  }

  /**
   * 增加商品数量
   * @param  id  order_cart的主键id
   */
  function increase(obj,id){
      $.post("{:url('MyCart/increase_cart')}",
          {id:id},
          function(data){
//              console.log(data);
              if(data.data.code == 1){
                  var goods_sum = parseInt($("#goods_sum_"+id).text());
                  var price = parseFloat($("#price_"+id).text());
                  goods_sum = goods_sum+1;
                  var total_price = parseFloat(goods_sum*price);
                  $("#goods_sum_"+id).text(goods_sum);
                  $("#goods_price_"+id).text(total_price);
              }else if(data.data.code == 0){
                  alert(data.msg);
              }else if(data.data.code == -1){            //商品已经下架，清空购物车该信息
                  alert(data.msg);
                  $(obj).parent().remove();         //移除商品信息
                  location.reload();
              }
          }
      );
  }

  /**
   * 删除购物车商品
   * 2018-03-30
   */
  function del(obj,id){
      $.post("{:url('MyCart/del')}",
          {id:id},
          function(data){
//              console.log(data);
              if(data.code == 1){
                  alert('删除成功');
                  $(obj).parent().remove();
                  location.reload();
              }else{
                  alert(data.msg);
              }
          }
      );
  }

  /**
   * 清空购物车
   */
  function qk_cart(){
      $.post("{:url('MyCart/qingkong')}",
          {id:1},
          function(data){
//              console.log(data);
              if(data.code == 1){
                  alert('清空成功');
                  $('#form').text('购物车空空如也~~~');
              }else{
                  alert(data.msg);
              }
          }
      );
  }

  /**
   * 检查表单数据
   */
  function checkform(){
      var ids = '';
      $.each($('input:checkbox:checked'),function(){
          ids += $(this).val()+',';
      });
      if($("input[type='checkbox']:checked").length == 0){
          alert('请选择需要购买的商品');
          return false;
      }
      $.post("{:url('MyCart/check_cart')}",
          {ids:ids},
          function(data){
              if(data.code == 0){
                  alert(data.msg);
                  $('#'+data.data).remove();        //移除元素
                  $('input[type=checkbox]').attr('checked',false);      //清空选择
                  location.reload();
                  return false;
              }else{
                  $('#form').submit();
//                  return true;
              }
          }
      );
      return false;
  }


</script>

